/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
 var diameterOfBinaryTree = function(root) {
    let res=0;
    function dfs(root){
        if(!root){
            return -1;
        }
        let maxwidth=Math.max(dfs(root.left)+1,dfs(root.right)+1);
        // console.log(maxwidth);
        res=Math.max(maxwidth,res);
        return maxwidth;
    }
    dfs(root);
    return res;
};